package com.houdoudou.supplier;

import android.content.Context;  
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.Button;
import android.widget.ImageView;  
import android.widget.TextView;
import java.util.ArrayList;  
import java.util.Arrays;
import java.util.List;  

import com.houdoudou.supplier.bean.FrgTag;
import com.houdoudou.supplier.common.CommonFunction;

public class SecondIndextAdapter extends BaseAdapter {
	
	Context context;
	List<String> secondIndextList;
	private LayoutInflater mInflater; 
	final int first_index_position;
	int current_selected_position=100;
	String[][] total_second_index={{"新增分类","搜索分类","新增产品","搜索产品"},{"当前任务","搜索订单"},{"待处理退款","搜索退款"},{"全部送货员","新增送货员"},{"全部送货员","新增送货员"},{"搜索自提柜"},{"修改密码","我是商家"}};
	public SecondIndextAdapter(Context context, int first_index) {
		
		this.context = context;
		this.first_index_position=first_index;
		 mInflater=LayoutInflater.from(context);  
		 secondIndextList=Arrays.asList(total_second_index[first_index]);
	}
	
	 @Override  
	    public int getCount() {  
	        return secondIndextList.size();  
	    }  
	     
	    @Override  
	    public Object getItem(int position) {  
	        return position;  
	    }  
	    private ViewHolder vh    =new ViewHolder();  
	  
	    private static class ViewHolder {  
	        private Button title ;  
	      
	    }  
	    @Override  
	    public long getItemId(int position) {  
	        return position;  
	    }  
	  
	    @Override  
	    public View getView(int position, View convertView, ViewGroup parent) {  
	    	final int second_index_position=position;
	        if(convertView==null){  
	            convertView = mInflater.inflate(R.layout.list_second_indext, null);  
	            vh.title=(Button)convertView.findViewById(R.id.title_list_secondindext);  
	            
	            convertView.setTag(vh);  
	        }else{  
	            vh=(ViewHolder)convertView.getTag();  
	        }  
	        vh.title.setText(secondIndextList.get(position)); 
	        if(current_selected_position==second_index_position)   vh.title.setBackgroundResource(R.drawable.second_index_selected_style);
	        vh.title.setOnClickListener(new View.OnClickListener() {
				
			
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					current_selected_position=second_index_position;
				
					Message msg = new Message();               
                    msg.what=2;
                   msg.arg1=first_index_position;
                   msg.arg2=second_index_position;
                  Bundle bundle= new Bundle(); 
                  bundle.putString("affair", getAffairName(first_index_position, second_index_position));
                  bundle.putString("tag",getTagName(first_index_position, second_index_position));
                  bundle.putString("title", getTitleName(first_index_position, second_index_position));
                  msg.obj=bundle;
                   SupplierMainActivity.myMainHandler.sendMessage(msg);
		
					
				}
			});
	        return convertView;  
	    }
	    
	    private String getAffairName(int firstIndex,int secondIndex){
	    	System.out.println(" firstIndex="+firstIndex+"     secondIndex="+secondIndex);
	    	String affair="";
	    	if((firstIndex==0)&&(secondIndex==0)) {affair="createIndex";}
	    	if((firstIndex==0)&&(secondIndex==2)) {affair="createProduct";}
	    	if((firstIndex==3)&&(secondIndex==1)) {affair="createCourier";}
	    	return affair;
	    }
	    private String getTagName(int firstIndex,int secondIndex){
	    	System.out.println(" firstIndex="+firstIndex+"     secondIndex="+secondIndex);
	    	String tag="";
	    	if((firstIndex==0)&&(secondIndex==0)) {tag=FrgTag.CREATE_SORT + CommonFunction.genRandomNum(10);}
	    	if((firstIndex==0)&&(secondIndex==1)) {tag=FrgTag.SEARCH_SORT;}
	    	if((firstIndex==0)&&(secondIndex==2)) {tag=FrgTag.CREATE_PRODUCT+CommonFunction.genRandomNum(10);}
	    	if((firstIndex==0)&&(secondIndex==3)) {tag=FrgTag.SEARCH_PRODUCT;}
	    	
	    	if((firstIndex==1)&&(secondIndex==0)) {tag=FrgTag.ORDER_DUTY;}
	    	
	    	if((firstIndex==2)&&(secondIndex==0)) {tag=FrgTag.REFUND_DUTY;}
	    	
	    	if((firstIndex==3)&&(secondIndex==1)) {tag=FrgTag.CREATE_COURIER;}
	    	return tag;
	    }
	    private String getTitleName(int firstIndex,int secondIndex){
	    	System.out.println(" firstIndex="+firstIndex+"     secondIndex="+secondIndex);
	    	String title="";
	    	if((firstIndex==0)&&(secondIndex==0)) {title="新增分类";}
	    	if((firstIndex==0)&&(secondIndex==1)) {title="管理分类";}
	    	if((firstIndex==0)&&(secondIndex==2)) {title="新增商品";}
	    	if((firstIndex==0)&&(secondIndex==3)) {title="管理商品";}
	    	
	    	if((firstIndex==1)&&(secondIndex==0)) {title="当前任务";}
	    	
	    	if((firstIndex==2)&&(secondIndex==0)) {title="待处理退款";}
	    	
	    	if((firstIndex==3)&&(secondIndex==1)) {title="新增送货员";}
	    	return title;
	    }

}
